<?php

namespace App\Http\Controllers\Dashboard;


use App\Models\LoanTag;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;


class LoanTagController extends BaseController
{
    /**
     * 列表
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request) {
        $pageSize = (int) $request->input('pageSize', 20);
        $query = LoanTag::ofOwner($request->user())->with(['org','user'])->withCount('loans')->orderByDesc('id');

        if ($title = $request->input('title')) {
            $query->where('title', 'like', "%{$title}%");
        }

        return response()->json($query->paginate($pageSize));
    }

    /**
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function detail(Request $request, $id)
    {
        $model =  LoanTag::ofOwner($request->user())->find($id);
        if (empty($model)) {
            return response()->json([
                'code' => 1,
                'msg' => '没有数据权限',
                'data' => null
            ]);
        } else {
            return response()->json([
                'code' => 0,
                'msg' => 'ok',
                'data' => $model
            ]);
        }
    }


    /**
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function edit(Request $request, $id)
    {
        $model =  LoanTag::ofOwner($request->user())->find($id);
        if (empty($model)) {
            return response()->json([
                'code' => 1,
                'msg' => '没有数据权限',
                'data' => null
            ]);
        }

        $model->title = trim($request->title);

        if($model->save()) {
            return response()->json([
                'code' => 0,
                'msg' => '保存成功',
                'data' => $model
            ]);
        } else {
            return response()->json([
                'code' => 1,
                'msg' => '保存失败',
                'data' => null
            ]);
        }
    }


    /**
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function create(Request $request)
    {
        if (empty($request->title)) {
            return response()->json([
                'code' => 1,
                'msg' => '名称不能为空',
                'data' => null
            ]);
        }

        $user = $request->user();
        $model = LoanTag::ofOwner($user)->where('title', $request->title)->first();
        if(!empty($model)) {
            return response()->json([
                'code' => 1,
                'msg' => '名称已经存在',
                'data' => null
            ]);
        }

        $model = new LoanTag();
        $model->title = $request->title;
        $model->org_root = $user->org_root;
        $model->org_code = $user->org_code;
        $model->user_id = $user->id;

        if ($model->save()) {
            return response()->json([
                'code' => 0,
                'msg' => 'ok',
                'data' => [
                    'id' => $model->id,
                    'title' => $model->title,
                ]
            ]);
        } else {
            return response()->json([
                'code' => 1,
                'msg' => '保存失败',
                'data' => null
            ]);
        }
    }

    /**
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function del(Request $request, $id) {
        $model =  LoanTag::ofOwner($request->user())->find($id);
        if (empty($model)) {
            return response()->json([
                'code' => 1,
                'msg' => '没有数据权限',
                'data' => null
            ]);
        }

        $model->loadCount('loans');

        if ($model->loans_count > 0) {
            return response()->json([
                'code' => 1,
                'msg' => '标签正在使用中，无法删除',
                'data' => null
            ]);
        }

        if($model->delete()) {
            return response()->json([
                'code' => 0,
                'msg' => '删除成功',
                'data' => null
            ]);
        } else {
            return response()->json([
                'code' => 1,
                'msg' => '删除失败',
                'data' => null
            ]);
        }
    }
}
